package com.simp.controller;

import com.simp.entity.SysCategoryEntity;
import com.simp.model.SysCategoryModel;
import com.simp.page.R;
import com.simp.service.SysCategoryService;
import com.simp.utils.BeanConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author pan.shun
 * @version 2.0
 * @since 2022/3/24 19:35
 */
@RestController
@RequestMapping("/category")
@Api(tags = "种类管理")
public class SysCategoryController {
    @Autowired
    private SysCategoryService sysCategoryService;

    @ApiOperation(value = "获取种类树集合")
    @GetMapping("/getTreeList/{tableName}")
    public R getTreeList(@ApiParam(name = "表名", value = "表名", required = true) @PathVariable String tableName) {
        return sysCategoryService.getTreeList(tableName);
    }

    @ApiOperation(value = "种类新增与删除", httpMethod = "POST")
    @PostMapping("/addOrUpdateCategory")
    public R addOrUpdateCategory(@RequestBody SysCategoryModel sysCategoryModel) {
        SysCategoryEntity convert = BeanConvertUtils.convert(sysCategoryModel, SysCategoryEntity.class);
        return sysCategoryService.addOrUpdateCategory(convert);
    }

    @ApiOperation(value = "通过ID删除种类", httpMethod = "GET")
    @GetMapping("/removeCategory/{id}")
    public R removeCategory(@ApiParam(name = "种类ID", value = "ID", required = true) @PathVariable String id) {
        return sysCategoryService.removeCategory(id);
    }
}
